package com.dubbo.lian.cms.modules.app.service.impl;
 import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.dubbo.lian.cms.modules.app.TbArticle;
import com.dubbo.lian.cms.modules.app.mapper.TbArticleMapper;
import com.dubbo.lian.cms.modules.app.service.ITbArticleService;
import com.dubbo.lian.common.util.BaseR;
import com.dubbo.lian.common.util.ReturnTemplateUtil;
import org.apache.commons.lang3.StringUtils;
import com.baomidou.kisso.common.encrypt.MD5;
import org.springframework.stereotype.Service;
 import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 /**
 * <p>
 * 文章  服务实现类
 * </p>
 *
 * @author lian
 * @since 2016-12-28
 */
@Service
public class TbArticleServiceImpl extends ServiceImpl<TbArticleMapper, TbArticle> implements ITbArticleService {
     @Override
    public BaseR selectArticleCarousel(JSONObject obj) {
        String userId =obj.getString("userid");
        System.out.println(userId);
        EntityWrapper ew = new EntityWrapper();
        ew. where("folder_id={0}",252);
        ew.and("is_recommend={0}",1);
        ew.and("status={0}","1");
        ew.orderBy("sort");
        List<TbArticle> selectArticleList = super.selectList(ew);
        Map articleListMap = new HashMap();
        List articleList = new ArrayList();
        selectArticleList.forEach(e-> {
            Map articleMap = new HashMap();
            articleMap.put("articleUrl","http://120.76.47.124:8080/jfinal_cms/front/article/"+e.getId()+".html"+"?u="+ MD5.toMD5(userId));
            articleMap.put("pictureUrl", "http://120.76.47.124:8080/jfinal_cms/"+StringUtils.replace(e.getImageUrl(),"\\","/"));
            articleList.add(articleMap);
        });
        articleListMap.put("articleList",articleList);
        return ReturnTemplateUtil.returnOfMap(articleListMap);
    }
     @Override
    public BaseR selectArticle(JSONObject obj) {
        String userId =obj.getString("userid");
        EntityWrapper ew = new EntityWrapper();
        String startTime = obj.getString("startTime");
        String endTime = obj.getString("endTime");
        ew.where("folder_id={0}",252);
        ew.and("status={0}","1");
         if(StringUtils.isNotBlank(startTime)&&!"null".equals(startTime)){
            ew.and("create_time>{0}",startTime);
        }
        if(StringUtils.isNotBlank(endTime)&&!"null".equals(endTime)){
            ew.and("create_time<{0}",endTime);
        }
        ew.orderBy("create_time");
        Page<TbArticle> page= new Page<>(1, 5);
        List<TbArticle> selectArticleList = super.selectPage(page,ew).getRecords();
        Map articleListMap = new HashMap();
        List articleList = new ArrayList();
        selectArticleList.forEach(e-> {
            Map articleMap = new HashMap();
            articleMap.put("articleUrl","http://120.76.47.124:8080/jfinal_cms/front/article/"+e.getId()+".html"+"?u="+ MD5.toMD5(userId));
            articleMap.put("pictureUrl", "http://120.76.47.124:8080/jfinal_cms/"+StringUtils.replace(e.getImageUrl(),"\\","/"));
            articleMap.put("introduce",e.getSummary());
            articleMap.put("title",e.getTitle());
            articleList.add(articleMap);
        });
        articleListMap.put("articleList",articleList);
        return ReturnTemplateUtil.returnOfMap(articleListMap);
    }
}
